package utils

import (
	"fmt"
	"os"
	"strings"
)

type MyArgs struct {
	ArgsMap map[string]string
}

func (m *MyArgs) GetByKey(key string) string {
	if nil == m.ArgsMap {
		fmt.Println("in===")
		m.ArgsMap = make(map[string]string)
		for i := 0; i < len(os.Args); i++ {
			if strings.HasPrefix(os.Args[i], "-") {
				if len(os.Args) == i+1 || strings.HasPrefix(os.Args[i+1], "-") {
					m.ArgsMap[os.Args[i]] = ""
				} else {
					m.ArgsMap[os.Args[i]] = os.Args[i+1]
				}
			}
		}
	}
	return m.ArgsMap[key]
}

func (m *MyArgs) GetByIndex(index int, defVal string) string {
	if index >= len(os.Args) {
		return defVal
	} else {
		return os.Args[index]
	}
}
